package co.fitstart.entity.course.round;

import java.beans.Transient;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.user.bwh.BwhRecord;
import co.fitstart.entity.user.inbody.InbodyRecord;

/**
 * 结营课次成果
 * @author haiyan_xiao
 *
 */
@Entity
@Table(name = "course_round_result")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
@DynamicUpdate
public class CourseRoundResult {
    @Id
    @GeneratedValue
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private CourseRound courseRound;
    
    /** 入营时三围 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private BwhRecord previousBwh;
    
    /** 最近三围 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private BwhRecord laterBwh;
    
    /** 入营时inbody */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private InbodyRecord previousInbody;
    
    /** 最近inbody */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private InbodyRecord laterInbody;
    
    /** 入营时照片 */
    private String previousPicture;
    
    /** 入营时照片(第几次上传) */
    private Integer previousVersion;

    /** 入营照上传时间 */
    private Date previousUploadTime;

    /** 结营时照片 */
    private String laterPicture;
    
    /** 结营时照片(第几次上传) */
    private Integer laterVersion;

    /** 结营照上传时间 */
    private Date laterUploadTime;

    public CourseRoundResult() {
        
    }
    
    public CourseRoundResult(CourseRound courseRound, BwhRecord previousBwh, BwhRecord laterBwh) {
        this.courseRound = courseRound;
        this.previousBwh = previousBwh;
        this.laterBwh = laterBwh;
    }
    
    public CourseRoundResult(CourseRound courseRound, InbodyRecord previousInbody, InbodyRecord laterInbody) {
        this.courseRound = courseRound;
        this.previousInbody = previousInbody;
        this.laterInbody = laterInbody;
    }
    
    public CourseRoundResult(CourseRound courseRound, BwhRecord previousBwh, BwhRecord laterBwh, InbodyRecord previousInbody, InbodyRecord laterInbody) {
        this.courseRound = courseRound;
        this.previousBwh = previousBwh;
        this.laterBwh = laterBwh;
        this.previousInbody = previousInbody;
        this.laterInbody = laterInbody;
    }
    
    public CourseRoundResult(CourseRound courseRound, String path, String suffix) {
        this.courseRound = courseRound;
        if("before".equals(suffix)) {
            this.previousPicture = path;
            this.previousUploadTime = new Date();
        } else if("after".equals(suffix)) {
            this.laterPicture = path;
            this.laterUploadTime = new Date();
        }
    }
    
    public CourseRoundResult(CourseRound courseRound, String path, Integer version, String suffix) {
        this.courseRound = courseRound;
        if("before".equals(suffix)) {
            this.previousPicture = path;
            this.previousVersion = version;
        } else if("after".equals(suffix)) {
            this.laterPicture = path;
            this.laterVersion = version;
        }
    }
    
    /**
     * 
     * @param previousBwh
     * @param laterBwh
     * @return
     */
    public CourseRoundResult edit(BwhRecord previousBwh, BwhRecord laterBwh) {
        this.previousBwh = previousBwh;
        this.laterBwh = laterBwh;
        return this;
    }
    
    /**
     * 
     * @param laterBwh
     * @return
     */
    public CourseRoundResult edit(BwhRecord laterBwh) {
        this.laterBwh = laterBwh;
        return this;
    }
    
    /**
     * 
     * @param previousInbody
     * @param laterInbody
     * @return
     */
    public CourseRoundResult edit(InbodyRecord previousInbody, InbodyRecord laterInbody) {
        this.previousInbody = previousInbody;
        this.laterInbody = laterInbody;
        return this;
    }

    /**
     * 
     * @param path
     * @param suffix
     * @return
     */
    public CourseRoundResult editPicture(String path, String suffix) {
        if("before".equals(suffix)) {
            if(this.previousPicture != null && this.previousVersion == null) {
                this.previousVersion = 1;
            } else if(this.previousPicture != null) {
                this.previousVersion++;
            }
            this.previousPicture = path;
            this.previousUploadTime = new Date();
        } else if("after".equals(suffix)) {
            if(this.laterPicture != null && this.laterVersion == null) {
                this.laterVersion = 1;
            } else if(this.laterPicture != null) {
                this.laterVersion++;
            }
            this.laterPicture = path;
            this.laterUploadTime = new Date();
        }
        return this;
    }

    /**
     * 
     * @param path
     * @param version
     * @param suffix
     * @return
     */
    public CourseRoundResult editPicture(String path, Integer version, String suffix) {
        if("before".equals(suffix)) {
            this.previousPicture = path;
            this.previousVersion = version;
        } else if("after".equals(suffix)) {
            this.laterPicture = path;
            this.laterVersion = version;
        }
        return this;
    }

    /**
     * 
     * @return
     */
    @Transient
    public Float getBustChange() {
        if(this.previousBwh != null && this.laterBwh != null
                && this.previousBwh.getBust() != null && this.laterBwh.getBust() != null) {
            return this.previousBwh.getBust() - this.laterBwh.getBust();
        } else {
            return null;
        }
    }

    /**
     * 
     * @return
     */
    @Transient
    public Float getWaistChange() {
        if(this.previousBwh != null && this.laterBwh != null
                && this.previousBwh.getWaist() != null && this.laterBwh.getWaist() != null) {
            return this.previousBwh.getWaist() - this.laterBwh.getWaist();
        } else {
            return null;
        }
    }

    /**
     * 
     * @return
     */
    @Transient
    public Float getHipsChange() {
        if(this.previousBwh != null && this.laterBwh != null
                && this.previousBwh.getHips() != null && this.laterBwh.getHips() != null) {
            return this.previousBwh.getHips() - this.laterBwh.getHips();
        } else {
            return null;
        }
    }

    /**
     * 
     * @return
     */
    @Transient
    public Float getWeightChange() {
        if(this.previousInbody != null && this.laterInbody != null
                && this.previousInbody.getWeight() != null && this.laterInbody.getWeight() != null) {
            return this.previousInbody.getWeight() - this.laterInbody.getWeight();
        } else {
            return null;
        }
    }

    /**
     * 
     * @return
     */
    @Transient
    public Float getBfrChange() {
        if(this.previousInbody != null && this.laterInbody != null
                && this.previousInbody.getBfr() != null && this.laterInbody.getBfr() != null) {
            return this.previousInbody.getBfr() - this.laterInbody.getBfr();
        } else {
            return null;
        }
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the courseRound
     */
    public CourseRound getCourseRound() {
        return courseRound;
    }

    /**
     * @param courseRound the courseRound to set
     */
    public void setCourseRound(CourseRound courseRound) {
        this.courseRound = courseRound;
    }

    /**
     * @return the previousBwh
     */
    public BwhRecord getPreviousBwh() {
        return previousBwh;
    }

    /**
     * @param previousBwh the previousBwh to set
     */
    public void setPreviousBwh(BwhRecord previousBwh) {
        this.previousBwh = previousBwh;
    }

    /**
     * @return the laterBwh
     */
    public BwhRecord getLaterBwh() {
        return laterBwh;
    }

    /**
     * @param laterBwh the laterBwh to set
     */
    public void setLaterBwh(BwhRecord laterBwh) {
        this.laterBwh = laterBwh;
    }

    /**
     * @return the previousInbody
     */
    public InbodyRecord getPreviousInbody() {
        return previousInbody;
    }

    /**
     * @param previousInbody the previousInbody to set
     */
    public void setPreviousInbody(InbodyRecord previousInbody) {
        this.previousInbody = previousInbody;
    }

    /**
     * @return the laterInbody
     */
    public InbodyRecord getLaterInbody() {
        return laterInbody;
    }

    /**
     * @param laterInbody the laterInbody to set
     */
    public void setLaterInbody(InbodyRecord laterInbody) {
        this.laterInbody = laterInbody;
    }

    /**
     * @return the previousPicture
     */
    public String getPreviousPicture() {
        return previousPicture;
    }

    /**
     * @param previousPicture the previousPicture to set
     */
    public void setPreviousPicture(String previousPicture) {
        this.previousPicture = previousPicture;
    }

    /**
     * @return the previousVersion
     */
    public Integer getPreviousVersion() {
        return previousVersion;
    }

    /**
     * @param previousVersion the previousVersion to set
     */
    public void setPreviousVersion(Integer previousVersion) {
        this.previousVersion = previousVersion;
    }

    /**
     * @return the previousUploadTime
     */
    public Date getPreviousUploadTime() {
        return previousUploadTime;
    }

    /**
     * @param previousUploadTime the previousUploadTime to set
     */
    public void setPreviousUploadTime(Date previousUploadTime) {
        this.previousUploadTime = previousUploadTime;
    }

    /**
     * @return the laterPicture
     */
    public String getLaterPicture() {
        return laterPicture;
    }

    /**
     * @param laterPicture the laterPicture to set
     */
    public void setLaterPicture(String laterPicture) {
        this.laterPicture = laterPicture;
    }

    /**
     * @return the laterVersion
     */
    public Integer getLaterVersion() {
        return laterVersion;
    }

    /**
     * @param laterVersion the laterVersion to set
     */
    public void setLaterVersion(Integer laterVersion) {
        this.laterVersion = laterVersion;
    }

    /**
     * @return the laterUploadTime
     */
    public Date getLaterUploadTime() {
        return laterUploadTime;
    }

    /**
     * @param laterUploadTime the laterUploadTime to set
     */
    public void setLaterUploadTime(Date laterUploadTime) {
        this.laterUploadTime = laterUploadTime;
    }
}
